/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#ifndef INTERFACEHOSTER_H
#define INTERFACEHOSTER_H

#include <QString>
#include <QUrl>
#include <QNetworkProxy>
#include <QPointer>
#include <QFile>
#include <QDir>
#include <QHttp>
#include <QTimer>
#include <QDateTime>
#include <QObject>

class InterfaceHoster
{
    public:

        enum E_MODE
        {
            MODE_FREE,
            MODE_PREMIUM,
            MODE_LOGIN,
            MODE_GET_SIZE
        };

        enum E_ERROR
        {
            ERROR_NO_ERROR,
            /*
            ERROR_HTTP,
            ERROR_FILE,
            ERROR_ATTR,
            ERROR_GET_VAR,
            ERROR_LOGIN,
            ERROR_GET_SIZE*/
        };

        enum E_STATE
        {
            STATE_STOPPED,
            STATE_FINISHED,
            STATE_RUNNING,
            STATE_HISTORY,
            STATE_STALLED,
            STATE_DOWNLOADING,
            STATE_WAITING,
            STATE_ERROR

            /*
            STATE_DELETE*/
        };

        /*! \struct S_SETTINGS
            \brief hoster settigns

            Contains the hoster settings.
         */
        struct S_SETTINGS
        {
            int maxDownloads;
        //	    int maxParts; // noch nicht implementieren
            int downloads;
            int maxConnections;

            S_SETTINGS() : maxDownloads(0), downloads(0), maxConnections(0) {};
            S_SETTINGS(int const &_maxDownloads, int const &_downloads, int const& _maxConnections) : maxDownloads(_maxDownloads), downloads(_downloads), maxConnections(_maxConnections) {};
        };

        struct S_INFO
        {
            QString name;
            QString host;

            int maxDownloads;
            bool allowOtherHostnames;

            QRegExp reHost;
            QRegExp reLink;

            S_SETTINGS free;
            S_SETTINGS premium;

            E_MODE mode;
        };

        InterfaceHoster() :  _id(-1), _fileSize(0), _downloaded(0), _progress(0), _downloadStart(QDateTime::currentDateTime()), _timeLeft(0), _logId(-1),
                    _errorStr(""), _speed(0), _state(STATE_STOPPED), _error(ERROR_NO_ERROR), _mode(MODE_FREE), smooth(20), speedIntervall(500), _file(""), _folder(""), host(""){};

        virtual ~InterfaceHoster() {};

        virtual QObject* clone() { return 0; };

        virtual InterfaceHoster::S_INFO info() const { return _info; };

        virtual bool setLink(QUrl const& url)
        {
            if(url.host().indexOf(this->_info.reHost) == -1)
                {
                    return false;
                } else
                {
                    this->_url = url;
                    return true;
                }
        }

        InterfaceHoster::E_STATE state() const { return _state; };
        InterfaceHoster::E_ERROR error() const { return _error; };
        InterfaceHoster::E_MODE mode() const { return _mode; };
        int progress() const { return _progress; };
        int timeLeft() const { return _timeLeft; };
        int logId() const { return _logId; };
        QString errorStr() const { return _errorStr; };
        double speed() const { return _speed; };
        QDateTime downloadStart() const { return _downloadStart; };
        QPointer<QFile> downloadFile() { return _downloadFile; };
        QString link() const {return _url.toString(); };
        virtual quint64 fileSize() const { return _fileSize; };
        virtual quint64 downloaded() const { return _downloaded; };



        virtual void setFile(QString const& file) { _file = file; };
        virtual void setFolder(QString const& folder) { _folder = folder; };
        virtual void setDownloadDir(QString const& downloadDir) { _downloadDir = downloadDir; };
        virtual void setId(int const& id) { _id = id; };
        virtual void setMode(E_MODE const& mode) { _mode = mode; };
        virtual void setLogId(int const& logId) { _logId = logId; };

        int setProxy(QNetworkProxy const &proxy) { return http.setProxy(proxy); };


        virtual void start() {};
        virtual void stop() {};
        virtual void login(QString const& user, QString const& pass) { Q_UNUSED(user); Q_UNUSED(pass); };
        virtual void getSize() {};

    protected:

        int _id;

        InterfaceHoster::S_INFO _info;

        quint64 _fileSize;
        quint64 _downloaded;
        int _progress;
        QDateTime _downloadStart;
        QDateTime _downloadEnd;
        int _timeLeft;
        int _logId;
        QString _errorStr;
        double _speed;
        QPointer<QFile> _downloadFile;

        E_STATE _state;
        E_ERROR _error;
        E_MODE _mode;

        QHttp http;

        //speed
        int smooth;
        int speedIntervall;
        ///speed

        QString _file;
        QString _folder;
        QDir _downloadDir;

        QUrl _url;
        QString host;
};

Q_DECLARE_INTERFACE(InterfaceHoster, "org.raptor-loader.Plugin.InterfaceHoster/1.0");

#endif // INTERFACEHOSTER_H
